package com.acwing.partition11;

import java.io.*;
import java.util.LinkedList;

/**
 * @author `RKC`
 * @date 2021/12/26 9:35
 */
public class AC1097池塘计数 {

    private static final int N = 1010, M = 1010;
    private static char[][] graph = new char[N][M];
    private static boolean[][] visited = new boolean[N][M];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] str = reader.readLine().split("\\s+");
        int n = Integer.parseInt(str[0]), m = Integer.parseInt(str[1]);
        for (int i = 0; i < n; i++) {
            String s = reader.readLine();
            for (int j = 0; j < m; j++) graph[i][j] = s.charAt(j);
        }
        writer.write(String.valueOf(resolve(n, m)));
        writer.flush();
    }

    private static int resolve(int n, int m) {
        int answer = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (graph[i][j] == '.' || visited[i][j]) continue;
                bfs(i, j, n, m);
                answer++;
            }
        }
        return answer;
    }

    private static void bfs(int x, int y, int n, int m) {
        //从当前位置向外扩散
        LinkedList<int[]> queue = new LinkedList<>();
        visited[x][y] = true;
        queue.addLast(new int[]{x, y});
        while (!queue.isEmpty()) {
            int[] curr = queue.pollFirst();
            x = curr[0];
            y = curr[1];
            for (int i = x - 1; i <= x + 1; i++) {
                for (int j = y -1; j <= y + 1; j++) {
                    if (i == x && j == y) continue;
                    if (i < 0 || i >= n || j < 0 || j >= m || graph[i][j] == '.' || visited[i][j]) continue;
                    queue.addLast(new int[]{i, j});
                    visited[i][j] = true;
                }
            }
        }
    }
}
